package com.example.lin.week2project;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.example.lin.week2project.adapter.WeChatAdpater;
import com.example.lin.week2project.pojo.QingChiFan;
import com.example.lin.week2project.utils.HttpUtils;
import com.example.lin.week2project.utils.JsonCache;
import com.google.gson.Gson;

import java.io.Serializable;

public class MainActivity extends AppCompatActivity {

    private static final String TAG ="MainActivity" ;
    private ListView lv_weChat;
//    private AlertDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();

        //判断是否已经缓存过 json 字符串
        String json = JsonCache.getJson(this);
        Log.d(TAG, "onCreate: 检测json是否缓存?"+json);
        if (json != null) {
            Log.i(TAG, "onCreate: json已经缓存,直接跳过网络请求...");
            QingChiFan qingChiFan = new Gson().fromJson(json, QingChiFan.class);
            bindAdapter2List(qingChiFan);
        } else {
            Log.d(TAG, "onCreate: 数据没有缓存,请求网络加载数据..");
            loadData();
        }
    }

    /**
     * 初始化视图
     */
    private void initView() {
        lv_weChat = findViewById(R.id.lv_weChat);

    }

    /**
     * 开始加载数据
     */
    private void loadData() {
//        dialog = new AlertDialog.Builder(this).setMessage("数据正在加载中...").create();
//        dialog.show();

        new Thread(new Runnable() {
            @Override
            public void run() {
                String address = "http://api.qingchifan.com/api/event/city.json?access_token=6916a7ab1f71e43ac4eadf40a51b4ab1E4DA4EE79DE4C64C449D09318FE0784F&basicCheck=0&city=0755&multi=0&gender=0&time=0&age=0&constellation=0&occupation=0&start=0&size=20&apiVersion=2.8.0";
                String s = HttpUtils.get(address);


                //保存json数据到File中
                Log.d(TAG, "run: 数据加载成功,将数据缓存到文件中");
                JsonCache.saveJson(MainActivity.this, s);


                QingChiFan qingChiFan = new Gson().fromJson(s, QingChiFan.class);
                Message message = new Message();
                message.what = 123;//给消息指定 编号
                Bundle bundle = new Bundle();
                bundle.putSerializable("sb", qingChiFan);
                message.setData(bundle);
                h.sendMessage(message);
            }
        }).start();
    }


    private Handler h = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            switch (msg.what) {
                case 123:
                    Bundle data = msg.getData();
                    Serializable sb = data.getSerializable("sb");
                    if (sb instanceof QingChiFan) {
                        QingChiFan qingChiFan = (QingChiFan) sb;
                        bindAdapter2List(qingChiFan);
                    }
                    //将弹出框 关闭掉
//                    dialog.dismiss();
                    Toast.makeText(MainActivity.this, "加载完成!", Toast.LENGTH_LONG).show();
                    break;
            }
            return false;
        }
    });

    private void bindAdapter2List(QingChiFan qingChiFan) {
        WeChatAdpater weChatAdpater = new WeChatAdpater(MainActivity.this, qingChiFan);
        lv_weChat.setAdapter(weChatAdpater);
    }


}
